package cn.haigui.icrawler.core;

import cn.hutool.core.collection.ConcurrentHashSet;

import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;

public class DefaultUrlManager implements IUrlManager{

    private ConcurrentLinkedDeque<String> target = new ConcurrentLinkedDeque<>();
    private ConcurrentHashSet<String> crawled = new ConcurrentHashSet<>();

    /**
     * 下一个抓取的链接
     *
     * @param context
     * @return
     */
    @Override
    public String next(Context context) {
        return target.poll();
    }

    /**
     * 添加已抓取的链接
     *
     * @param context
     * @param url
     */
    @Override
    public void addCrawled(Context context, String url) {
        if (crawled.contains(url)) {
            return;
        }
        crawled.add(url);
    }

    /**
     * 添加待抓取的链接
     *
     * @param context
     * @param urls
     */
    @Override
    public void addTarget(Context context, List<String> urls) {
        urls.forEach(e->addTarget(context,e));
    }

    /**
     * 添加待抓取的链接
     *
     * @param context
     * @param url
     */
    @Override
    public void addTarget(Context context, String url) {
        if (target.contains(url)) {
            return;
        }
        if (crawled.contains(url)) {
            return;
        }
        target.add(url);
    }

    /**
     * 添加待抓取的链接到首部
     *
     * @param context
     * @param url
     */
    @Override
    public void addTargetToFirst(Context context, String url) {
        if (target.contains(url)) {
            return;
        }
        if (crawled.contains(url)) {
            return;
        }
        target.addFirst(url);
    }

    /**
     * 添加待抓取的链接到首部
     *
     * @param context
     * @param urls
     */
    @Override
    public void addTargetsToFirst(Context context, List<String> urls) {
        urls.forEach(e->addTargetToFirst(context,e));
    }
}
